package ca.usask.cs.Gui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import  org.jdesktop.layout.GroupLayout;


/**
 * A class to import Vensim Model used by textbox to get the location of the model
 * @author Wenyi An
 */
	public class LoadVensimGui extends JFrame {
		private InterfaceHandler interfaceHandler;
	
	    public LoadVensimGui(InterfaceHandler interfaceHandler) {
	    	this.interfaceHandler=interfaceHandler;
	        initComponents();
	    }

	    /** This method is called from within the constructor to
	     * initialize the form.
	     * 
	     */
	    private void initComponents() {
	    	createSwingComponents();
	    	setTextOnComponents();
	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	        AddactionListener();          
	        GroupLayout layout = new GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layoutHorizontalSet(layout);
	        layoutVertialSet(layout);       
	        pack();        
	        this.setLocationRelativeTo(null);
	    }
	  
	    // Variables declaration - do not modify
	    private JButton browseFileBotton;
	    private JButton useExistTreeBotton;
	    private JButton quitBotton;
	    private JButton createTreeBotton;
	    private JLabel welcomeMsg;
	    private JLabel specifyMsg;
	    public JTextField vensimLocation;
	    private JLabel errorLabel;
		boolean buttonClick =false;
	    // End of variables declaration
	    
	    private void createSwingComponents(){
	    	welcomeMsg = new JLabel();
	        specifyMsg = new JLabel();
	        vensimLocation = new JTextField();
	        browseFileBotton = new JButton();
	        useExistTreeBotton = new JButton();
	        quitBotton = new JButton();
	        createTreeBotton = new JButton();
	        errorLabel = new JLabel();
	    }
	    
	    private void setTextOnComponents(){
	    	setTitle("Decision Tree");		      
	        welcomeMsg.setFont(new java.awt.Font("Tahoma", 0, 18)); 
	        welcomeMsg.setText("Welcome to the DecisionTree Wizard");
	        specifyMsg.setText("Please specify the Vensim Model to Use (this should be a .vpm file \"published\" by Vensim's File/Publish menu item)");
	        vensimLocation.setText("C:\\tmp\\WNVDecisionTree.vpm");
	        browseFileBotton.setText("Browse");
	        useExistTreeBotton.setText("Use Existing Tree");
	        quitBotton.setText("Quit");
	        createTreeBotton.setText("Create New Tree");
	        errorLabel.setForeground(new Color(255, 0, 0));
	        errorLabel.setText("");
	        
	    }
	    /**
	     * Add different Listener for different component
	     */
	    private void  AddactionListener(){
	    	browseFileBotton.addActionListener(new FileChooserListener());
		    useExistTreeBotton.addActionListener(new UseExistingTreeListener());            
		    quitBotton.addActionListener(new ExitListener());	       
		    createTreeBotton.addActionListener(new CreateNewTreeListener());	
	    }
	    
	    /**
		 * The listener for the press of the submit button.
		 * When it happens, obtain the data values from the fields, and
		 * create the interface of existingTree and init the vensim model
		 */
		private class UseExistingTreeListener implements ActionListener
		{
			public void actionPerformed (ActionEvent event)
			{
					if(vensimLocation.getText().trim().length() != 0){  // empty String						
					    if(NotVensimCheck()==false){
					    	return;
					    }					
						else{
							File file = new File(vensimLocation.getText().trim());						
							if(ExistCheck(file)==false){
						    	return;
						    }	
							else{
								errorLabel.setText("");	
								setVisible(false);
								interfaceHandler.InitVensim(vensimLocation.getText().trim());
								}	
						}
						
						interfaceHandler.createExistingTreeInterface();
					}
					else{
						errorLabel.setText("* Empty field");
						return;
					}
			}
			
		
			
		}
		private boolean NotVensimCheck(){
			if(!vensimLocation.getText().trim().endsWith(".vpm")){
				errorLabel.setText("* Not an vpm file");
				return false;
			}
			else
				return true;
		}
		
		private boolean ExistCheck(File file){
			if(!file.exists()&&!file.isFile()){
				errorLabel.setText("* Not an exist vensim model" );
				return false;
			}
			else
				return true;
		}
		/**
		 * The listener for the press of the exit button.
		 * When it happens, the process will be closed
		 */
		
		private class ExitListener implements ActionListener
		{
			public void actionPerformed (ActionEvent event)
			{
				System.exit(0);
			}
		}
		/**
		 * The listener for the press of the submit button.
		 * When it happens, obtain the data values from the fields, init the vensim model
		 * and create the interface of CreateNewTree. 
		 */

		private class CreateNewTreeListener implements ActionListener
		{				
			public void actionPerformed (ActionEvent event)
			{
					if(vensimLocation.getText().trim().length() != 0){	
						if(NotVensimCheck()==false){
					    	return;
					    }
						else{
							File file = new File(vensimLocation.getText().trim());
							if(ExistCheck(file)==false){
						    	return;
						    }	
							else{
								errorLabel.setText("");	
								setVisible(false);
								interfaceHandler.InitVensim(vensimLocation.getText().trim());
								}	
						}
						
						interfaceHandler.createNewTreeInterface();
					}
					else{
						errorLabel.setText("* Empty field");
						return;
					}				
			}
		}
		
		 /**
		 * The listener for choose file from the user's computer
		 * The user can choose the vensim file they want
		 */		
		private class FileChooserListener implements ActionListener
		{	
			public void actionPerformed(ActionEvent e) {
				   JFileChooser c = new JFileChooser();
				   int rVal = c.showOpenDialog(LoadVensimGui.this);
				   if (rVal == JFileChooser.APPROVE_OPTION) {
					   vensimLocation.setText(c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName());
				   }
				   if (rVal == JFileChooser.CANCEL_OPTION) {
					   vensimLocation.setText("C:\\tmp\\WNVDecisionTreeV22.vpm");			    
				   }
				 }
		}
		/**
		 * This method checks if the vemsim model is end with ".vpm"
		 */
	
		
		
	
		private void layoutHorizontalSet(GroupLayout layout){
			layout.setHorizontalGroup(
		            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		            .add(layout.createSequentialGroup()
		                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                    .add(layout.createSequentialGroup()
		                        .add(131, 131, 131)
		                        .add(specifyMsg))
		                    .add(layout.createSequentialGroup()
		                        .add(133, 133, 133)
		                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
		                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
		                            		.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
		                                            .add(errorLabel, org.jdesktop.layout.GroupLayout.LEADING, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, /* 204, */ Short.MAX_VALUE)
		                            		.add(vensimLocation))
		                                .add(18, 18, 18)
		                                .add(browseFileBotton))
		                            .add(welcomeMsg))))
		                .addContainerGap(132, Short.MAX_VALUE))
		            .add(layout.createSequentialGroup()
		                .add(53, 53, 53)
		                .add(useExistTreeBotton)
		                .add(55, 55, 55)
		                .add(quitBotton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 59, Short.MAX_VALUE)
		                .add(createTreeBotton)
		                .add(51, 51, 51))
		        );
		}
		private void layoutVertialSet(GroupLayout layout){
			 layout.setVerticalGroup(
			            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			            .add(layout.createSequentialGroup()
			                .add(64, 64, 64)
			                .add(welcomeMsg)
			                .add(34, 34, 34)
			                .add(specifyMsg)
			                .add(30, 30, 30)
			                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
			                    .add(vensimLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
			                    .add(browseFileBotton))
			                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(errorLabel)
                .add(42, 42, 42)
			                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
			                    .add(useExistTreeBotton)
			                    .add(quitBotton)
			                    .add(createTreeBotton))
			                .addContainerGap(35, Short.MAX_VALUE))
			        );
		}
		public static final long serialVersionUID = 1;
	}

